<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class ShopGoods extends Model
{
    use SoftDelete;

    protected $autoWriteTimestamp = 'dateTime';

    protected $append = ['rebate_name', 'creden_imgs'];

    public static $free_queuing_types = [
        1 => '正价购买',
        2 => '排队免单',
        3 => '排队+正价',
    ];

    public static $service_types = [
        1 => '包含配送',
        2 => '自提',
        3 => '配送+自提',
    ];

    protected static function init()
    {
        self::beforeInsert(function ($data) {
            if (empty($data['sort'])) {
                $data['sort'] = static::max('sort') + 5;
            }
        });

        self::beforeWrite(function ($data) {
            if (empty($data['images'])) {
                $data['images'] = isset($data['logo']) ? $data['logo'] : '';
            }
        });
    }

    public function getAdminGroupAttr($value, $data)
    {
        $model = new \tpext\myadmin\admin\model\AdminUser;
        $model = $model->getAdminGroupModel();
        $category = $model->get($data['admin_group_id']);
        return $category ? $category['name'] : '--';
    }

    public function setTags($value)
    {
        if (empty($value)) {
            return '';
        }

        return is_array($value) ? ',' . implode(',', $value) . ',' : ',' . trim($value, ',') . ',';
    }

    public function getAttrAttr($value, $data)
    {
        $attr = [];
        if ($data['is_recommend']) {
            $attr[] = 'is_recommend';
        }
        if ($data['is_hot']) {
            $attr[] = 'is_hot';
        }
        if ($data['is_top']) {
            $attr[] = 'is_top';
        }

        return $attr;
    }

    public function getSpuAttr($value, $data)
    {
        if (empty($value) && isset($data['id'])) {
            return 'sp' . str_pad($data['id'], 7, "0", STR_PAD_LEFT);
        }

        return $value;
    }

    public function getManyPriceAttr($value, $data)
    {
        return ShopGoodsSpecPrice::where(['goods_id' => $data['id']])->count() > 0 ? 1 : 0;
    }

    public function getRebateNameAttr($value, $data)
    {
        return RebateConfig::where(['id' => $data['rc_id']])->value('name') ?? '';
    }

    public function getCredenImgsAttr($value, $data)
    {
        $creden_ids = stringToArray($data['for_certs']);

        $creden_imgs = [];
        $list = StoreCredential::where('id', 'in', $creden_ids)->select();
        foreach ($list as &$item) {
            $imgs = stringToArray($item['certificate_file']);
            $creden_imgs = array_merge($imgs);
        }
        return $creden_imgs;
    }

    public function cate()
    {
        return $this->belongsTo('store_goods_cate', 'store_goods_cate_id', 'id');
    }

    public function unit()
    {
        return $this->belongsTo('shop_goods_unit', 'unit_id', 'id');
    }

    public function category()
    {
        return $this->belongsTo(ShopCategory::class, 'category_id', 'id');
    }

    public function brand()
    {
        return $this->belongsTo(ShopBrand::class, 'brand_id', 'id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }
}
